package com.fire.web.modules.all.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.fire.common.mybatis.mvc.wrapper.EntityWrapper;
import com.fire.web.modules.all.entity.UserAuthorityModel;
import com.fire.web.modules.all.mapper.UserAuthorityMapper;
import com.fire.web.modules.all.service.*;
import com.fire.web.modules.all.utils.WrapperUtil;
import com.fire.web.modules.all.constant.ErrorCodeConstants;
import com.fire.web.modules.all.entity.AjaxResult;
import com.fire.web.modules.all.entity.FireProprietorUserModel;
import com.fire.web.modules.all.request.AddUserReq;
import com.fire.web.modules.all.utils.TokenUtil;
import com.fire.web.modules.sso.service.IOAuthService;
import com.fire.web.security.shiro.realm.UserRealm;
import com.fire.web.utils.PageRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


@RestController
@RequestMapping("/proprietor/user")
public class ProprietorUserController extends CrudController<IFireProprietorUserService, FireProprietorUserModel> {
	private static final Logger LOGGER = LoggerFactory.getLogger(ProprietorUserController.class);

	@Autowired
	ITMitErrorCodeService errorCodeService;
	@Autowired
	IFireProprietorUserService fireProprietorUserService;
	@Autowired
	IAllUserService allUserService;
	@Autowired
	ISequenceService sequenceService;
	@Autowired
	IOAuthService oAuthService;
	@Autowired
	IUserAuthorityService userAuthorityService;

	/**
	 * 查询列表
	 *
	 * @return
	 */
	@GetMapping("/getProUserList")
	public AjaxResult getProUserList(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
		AjaxResult ajaxResult;
		try {
			String token = TokenUtil.getToken(request, response);
			UserRealm.Principal principalByAccessToken = oAuthService.getPrincipalByAccessToken(token);
			String id = principalByAccessToken.getId();
			String username = principalByAccessToken.getUsername();

			//遍历参数获得查询条件
			EntityWrapper ew = WrapperUtil.getEw(params);
			//查询
			Page pageBean = null;
			String adminStr = "admin";
			if(adminStr.equals(username)){
				pageBean = fireProprietorUserService.selectPage(PageRequest.getPage(), ew);
			} else {
				pageBean = fireProprietorUserService.selectPage(PageRequest.getPage(), ew.eq("adminid", id));
			}

			ajaxResult = new AjaxResult(ErrorCodeConstants.SUCCESS, "获取成功", pageBean, "获取成功");
		} catch (Exception e) {
			LOGGER.error("ProprietorUserController.getProUserList().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);

		return ajaxResult;
	}

	/**
	 * 增加业主单位用户
	 *
	 * @return
	 */
	@PostMapping("/addProUser")
	@ResponseBody
	public AjaxResult addProUser(AddUserReq req, HttpServletRequest request, HttpServletResponse response) {
		LOGGER.info("==================新增业主单位用户开始==================");
		LOGGER.info(req.toString());
		AjaxResult ajaxResult;
		try {
			String token = TokenUtil.getToken(request, response);
			UserRealm.Principal principalByAccessToken = oAuthService.getPrincipalByAccessToken(token);
			String adminid = principalByAccessToken.getId();
			ajaxResult = fireProprietorUserService.addProUser(req,adminid);
		} catch (Exception e) {
			LOGGER.error("ProprietorUserController.addProUser().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);
		LOGGER.info("==================新增业主单位用户结束==================");
		return ajaxResult;
	}

	/**
	 * 更新业主单位用户
	 *
	 * @return
	 */
	@PutMapping("/updateProUser")
	@ResponseBody
	public AjaxResult updateProUser(AddUserReq req) {
		LOGGER.info("==================更新业主单位用户开始==================");
		LOGGER.info(req.toString());
		AjaxResult ajaxResult;
		try {
			ajaxResult = fireProprietorUserService.updateProUser(req);
		} catch (Exception e) {
			LOGGER.error("ProprietorUserController.updateProUser().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);
		LOGGER.info("==================更新业主单位用户结束==================");
		return ajaxResult;
	}


	/**
	 * 删除
	 *
	 * @return
	 */
	@DeleteMapping("/deleteProUser/{id}")
	@ResponseBody
	public AjaxResult deleteProUser(@PathVariable("id") String id) {
		AjaxResult ajaxResult;
		try {
			allUserService.deleteById(Integer.parseInt(id));
			fireProprietorUserService.deleteById(Integer.parseInt(id));
			ajaxResult = new AjaxResult(ErrorCodeConstants.SUCCESS, "删除成功", "删除成功");
		} catch (Exception e) {
			LOGGER.error("ProprietorUserController.deleteProUser().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);

		return ajaxResult;
	}

	/**
	 * 获得用户权限
	 *
	 * @return
	 */
	@GetMapping("/getAuthority/{id}")
	@ResponseBody
	public AjaxResult getAuthority(@PathVariable("id") String id) {
		AjaxResult ajaxResult;
		try {
			UserAuthorityModel userAuthorityModel = userAuthorityService.selectById(id);
			ajaxResult = new AjaxResult(ErrorCodeConstants.SUCCESS, "获取权限成功",userAuthorityModel, "获取权限成功");
		} catch (Exception e) {
			LOGGER.error("ProprietorUserController.getAuthority().error  错误为：", e);
			ajaxResult = new AjaxResult(ErrorCodeConstants.INTERNAL_ERROR, "系统内部错误", "系统内部错误");
		}
		//替换tips
		ajaxResult = replaceTips(ajaxResult);

		return ajaxResult;
	}
}
